<template>
  <div class="app-container">
    <el-row :gutter="20">
      <el-col :span="12" :xs="24">
        <el-card class="box-card">
          <div slot="header" class="clearfix">
            <span>个人信息</span>
          </div>
          <div>
            <div class="text-center">
              <userAvatar />
            </div>
            <ul class="list-group list-group-striped">
              <li class="list-group-item">
                <svg-icon icon-class="user" />用户名称
                <div class="pull-right">{{ user.realName }}
                 <i class="el-icon-edit" @click="changePhone('pwd')" style="color: rgba(0, 170, 255, 1.0);margin-left: 0.2cm;"/>
                </div>
              </li>
              <li class="list-group-item">
                <svg-icon icon-class="phone" />手机号码
                <div class="pull-right">{{ user.phonenumber }}
                  <i class="el-icon-edit" @click="changePhone('phone')" style="color: rgba(0, 170, 255, 1.0);margin-left: 0.2cm;"/>
                </div>
              </li>
              <li class="list-group-item">
                <svg-icon icon-class="email" />用户邮箱
                <div class="pull-right">{{ user.email }}
                     <i class="el-icon-edit" @click="changePhone('email')" style="color: rgba(0, 170, 255, 1.0);margin-left: 0.2cm;"/>
                </div>
              </li>
              <li class="list-group-item"  v-hasPermi="['profile:org']">
                <svg-icon icon-class="tree" />所属部门
                <div class="pull-right" v-if="user.orgName">{{ user.orgName }}</div>
              </li>
              <li class="list-group-item" v-hasPermi="['profile:role']">
                <svg-icon icon-class="peoples" />所属角色
                <div class="pull-right">{{ user.roleName }}</div>
              </li>
              <li class="list-group-item">
                <svg-icon icon-class="date" />创建日期
                <div class="pull-right">{{ user.createTime }}</div>
              </li>
            </ul>
          </div>
        </el-card>
      </el-col>

    </el-row>

    <el-dialog :title="title" :visible.sync="open" width="350px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row v-if="showFlag === 'phone'">
          <el-col :span="24">
            <el-form-item label="手机号" prop="phonenumber">
              <el-input v-model="form.phonenumber" placeholder="请输入手机号" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row v-if="showFlag === 'email'">
          <el-col :span="24">
            <el-form-item label="邮箱" prop="email">
              <el-input v-model="form.email" placeholder="请输入邮箱" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row v-if="showFlag === 'pwd'">
          <el-col :span="24">
            <el-form-item label="新的密码" prop="password">
              <el-input  type="password" v-model="form.password" placeholder="请输入新的密码" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import userAvatar from "./userAvatar";
import userInfo from "./userInfo";

import { getUserProfile ,updateUser,resetMyPwd} from "@/api/system/user";

export default {
  name: "Profile",
  components: { userAvatar, userInfo },
  data() {
    return {
      user: {},
      activeTab: "userinfo",
      title:'',
      open:false,
      form:{},
      showFlag:'',
      rules: {

        email: [
          {
            required: true,
            type: "email",
            message: "'请输入正确的邮箱地址",
            trigger: ["blur", "change"]
          }
        ],
        phonenumber: [
          {
            required: true,
            pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
            message: "请输入正确的手机号码",
            trigger: "blur"
          }
        ],
        password: [
          {
            required: true,
            message: "请输入新的密码",
            trigger: "blur"
          }
        ]
      }
    };
  },
  created() {
    this.getUser();
  },
  methods: {
    getUser() {
      getUserProfile().then(response => {
        this.user = response.data;
      });
    },
    changePhone(value){
      this.form = JSON.parse(JSON.stringify( this.user))
      if(value === 'phone'){
        this.title ="修改手机号";
        this.showFlag = 'phone';
        this.form.phonenumber = '';
      }else if(value === 'email'){
        this.title ="修改邮箱";
        this.showFlag = 'email';
        this.form.email ='';
      }else if(value === 'pwd'){
        this.title ="修改密码";
        this.showFlag = 'pwd';
        this.form.password ='';
      }
      this.open = true;

    },
    submitForm: function() {
      this.$modal.loading();
      this.$refs["form"].validate(valid => {
        if (valid) {
            updateUser(this.form).then(res =>{
              this.open = false;
              this.getUser();
              this.$modal.closeLoading();
            })
        }
      });
    },
    cancel(){
      this.open = false
    }
  }
};
</script>
